<template>
    <div class="login-template">
        <div class="login-logo">
            <yd-icon slot="icon" name="home" color="#FF685D" size="1.5rem"></yd-icon>
        </div>
        <yd-cell-group>
            <yd-cell-item>
                <span slot="left">用户名：</span>
                <yd-input slot="right" required v-model="postDate.userName" max="20" ref="userName" placeholder="请输入用户名"></yd-input>
            </yd-cell-item>
            <yd-cell-item>
                <span slot="left">密码：</span>
                <yd-input slot="right" type="password" v-model="postDate.password" ref="password" placeholder="请输入密码" required></yd-input>
            </yd-cell-item>
            <!--<yd-button-group>
                <yd-button size="large" @click.native="loginBtn">登录</yd-button>
            </yd-button-group>-->
        </yd-cell-group>
        <div class="m-button"><button class="btn-block btn-primary" @click="loginBtn">登录</button></div>
    </div>
</template>
<script>
    import auth from '../service/auth.js'
    import Ajax from "../api/ajax.js"
    import dataURL from "../data/dataURL.js"
    import Md5 from 'js-md5'
    export default {
        data() {
            return {
                postDate: {
                    userName: '',
                    password: ''
                }
            }
        },
        methods: {
            async loginBtn() {
                for (var i in this.$refs) {
                    if (!this.$refs[i].valid) {
                        this.$dialog.toast({
                            mes: '请正确输入账号密码',
                            timeout: 500,
                        });
                        return;
                    }
                }
                let res = await Ajax.Post(dataURL.login, {
                    user_name: this.postDate.userName,
                    password: Md5(this.postDate.password)
                });

                console.log(res);
                console.log(res.token)
                auth.setLogged(res.token);
                this.$router.push('/');
            }
        }
    }

</script>